<?php
/**
 * @package		Joomla.Site
 * @copyright	Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

jimport('joomla.application.categories');

function ApkipasBuildRoute(&$query)
{
    $segments = array ();
    $menu = & JSite::getMenu();
    if ( empty($query['Itemid'])) {
        $menuItem = & $menu->getActive();
    }
    else {
        $menuItem = & $menu->getItem($query['Itemid']);
    }
 
    $mView = ( empty($menuItem->query['view']))?null:$menuItem->query['view'];
    $mId = ( empty($menuItem->query['id']))?null:$menuItem->query['id'];
    $mTag = ( empty($menuItem->query['tag']))?null:$menuItem->query['tag'];
	
   
    if ( isset($query['view']) && isset ($query['start'])){
    	if($query['view'] == 'apkipas')
    		unset ($query['view']);
    }
    
    if (isset($query['view']) && isset ($query['limitstart'])){
    	if($query['view'] == 'apkipas')
    		unset ($query['view']);
    }
    
    if ($mView == @$query['view'] )
    {
    	$segments[] = $query['view'];
    	unset ($query['view']);
    }
    
    if ( isset ($query['layout'])) {
    	$segments[] = $query['layout'];
    	unset ($query['layout']);
    }
    
    if ( isset ($query['id'])) {
    	$segments[] = str_ireplace(intval($query['id']).':', '', $query['id']);
    	$segments[] = intval($query['id']);
    }

    if ( isset ($query['tag'])) {
        $segments[] = $query['tag'];
        unset ($query['tag']);
    }
    
    if ( isset ($query['start'])) {
    	$segments[] = $query['start'];
    	unset ($query['start']);
    }
    
    if ( isset ($query['limitstart'])) {
    	unset ($query['limitstart']);
    }
    
    unset ($query['id']);
    return $segments;
}

function ApkipasParseRoute($segments) {	
    $vars = array ();

    if ($segments[0] == 'apkipas') {
    	$vars['view'] = $segments[0];
        switch($segments[1]) {
            default:
                $vars['id'] = $segments[2];
                break;
        }
    }

    else if ($segments[0] == 'detail') {
    	$vars['view'] = $segments[0];

        switch($segments[1]) {
        	case 'tag':
        		$vars['layout'] = $segments[1];
        		$vars['tag'] = $segments[2];
        		$vars['limitstart'] = $segments[3];
        		break;
            default:
                $vars['id'] = $segments[2];
                break;
        }
    }
    
    else {
    	$vars['view'] = 'apkipas';
    	$vars['limitstart'] = $segments[0];
    }

    return $vars;
}

